package top.oneit.jdownload.config;


import cn.hutool.core.util.ClassUtil;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;

/**
 * @author lingkang
 * @date 2021/11/19
 */
public class DbConfig {
    public static SessionFactory init() {
        String url = "jdbc:h2:" + getPath() + "jdownload.db";
        // 配置属性：https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#configurations
        Properties properties = new Properties();
        properties.put("hibernate.connection.driver_class", "org.h2.Driver");
        properties.put("hibernate.connection.url", url);
        properties.put("hibernate.connection.username", "sa");
        properties.put("hibernate.connection.password", "123456");
        properties.put("hibernate.hbm2ddl.auto", "update");
        properties.put("hibernate.show_sql", "true");
        properties.put("hibernate.connection.autocommit", "true");
        properties.put("connection.pool_size", 20);
        properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        properties.put("hibernate.current_session_context_class", "thread");

        Configuration configuration = new Configuration();
        configuration.setProperties(properties);
        // 加载实体映射
        List<Class<?>> tableClass = getTableClass("top.oneit.jdownload.data.entity");
        for (Class clazz : tableClass) {
            configuration.addAnnotatedClass(clazz);
        }

        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(properties)
                .build();
        try {
            SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            sessionFactory.openSession();

            return sessionFactory;
        } catch (Exception e) {
            e.printStackTrace();
            StandardServiceRegistryBuilder.destroy(serviceRegistry);
        }
        return null;
    }

    public static String getPath() {
        String path = DbConfig.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        if (System.getProperty("os.name").contains("dows")) {
            path = path.substring(1, path.length());
        }
        if (path.contains("jar")) {
            path = path.substring(0, path.lastIndexOf("."));
            return path.substring(0, path.lastIndexOf("/"));
        }
        return path.replace("target/classes/", "");
    }

    public static void main(String[] args) {
        Set<Class<?>> classes = ClassUtil.scanPackage("top.oneit.jdownload.entity");
        System.out.println(classes);
        for (Class<?> c : classes) {
            System.out.println(c.getAnnotation(Table.class));
        }
    }

    private static List<Class<?>> getTableClass(String packageName) {
        Set<Class<?>> classes = ClassUtil.scanPackage(packageName);
        List<Class<?>> list = new ArrayList<>();
        for (Class<?> clazz : classes) {
            if (clazz.getAnnotation(Table.class) != null) {
                list.add(clazz);
            }
        }
        return list;
    }

}
